home *** CD-ROM | disk | FTP | other *** search
/ SPACE 2 / SPACE - Library 2 - Volume 1.iso / program / 138 / pascal / randnums.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1987-05-13  |  1.3 KB  |  55 lines

  1. {RANDOM NUMBER GENERATOR             GARY CURTIS NEWPORT
  2.  
  3.     PURPOSE : To generate a pseudorandom number in the open interval, (0,1).
  4.  
  5.     ALGORITHM : The linear congruential method is used.  See G. Gordon,
  6.                 System Simulation, second edition, Prentice-Hall, 1978,
  7.                 pp. 128-129
  8.  
  9.     USE :   The following global declaration is necessary:
  10.  
  11.             VAR seed : long_integer;
  12.  
  13.             Include RANDNUMS after program declarations.  At the beginning
  14.             of the program, insert the following:
  15.  
  16.             SET_RANDOM(seed);
  17.  
  18.             This seeds the random number generator, which can be called
  19.             as needed, in  the following way:
  20.  
  21.             x := RANDOM(seed);
  22.  
  23.            (Where x is any declared real variable.)
  24.  
  25. }
  26.  
  27.  procedure
  28.  SET_RANDOM ( VAR seed : long_integer);
  29.  
  30.     CONST
  31.  
  32.        m = 65535;  { must be >= 0 and < 262139 }
  33.  
  34.  BEGIN {Set_Random}
  35.  
  36.  seed := CLOCK mod m
  37.  
  38.  END; {Set_Random}
  39.  
  40.  function
  41.  RANDOM ( VAR seed : long_integer): real;
  42.  
  43.     CONST
  44.  
  45.        a = 11109;   { See Daley, lecture notes, Computer Simulation, p. 19 }
  46.        c = 13849;   {      for a derivation of these constants.            }
  47.        m = 65535;
  48.  
  49.   BEGIN {Random}
  50.  
  51.   seed := ((seed * a) + c) mod m;
  52.   Random := seed/m
  53.  
  54.   END; {Random}
  55.